package com.lyd.refelction;

import java.io.*;
import java.lang.reflect.Field;

/**
 * @Author:lyd
 **/
public class RefectionDemo {
    public static void main(String[] args) throws Exception {

        Dog dog = new Dog("brain", 5, "吃骨头");
        Man stark = new Man("stark", 24, 9888.0);

        saveData(dog);
        saveData(stark);
    }

    //
    public static void saveData(Object obj) throws Exception {
        BufferedWriter bw = new BufferedWriter(new FileWriter("java-basic/src/obj.txt",true));
        Class<?> tClass =obj.getClass();
        String typeName = tClass.getSimpleName();
        bw.write("============"+typeName+"============");
        bw.newLine();
        Field[] fields = tClass.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            field.setAccessible(true);
            Object value = field.get(obj);
            String str=name+"="+value;
            bw.write(str);
            bw.newLine();
        }
        bw.close();
    }
}
